package com.xingda.packing.events;

import com.google.common.eventbus.EventBus;
import com.xingda.packing.config.appset.AppSetParam;
import com.xingda.packing.enums.*;
import com.xingda.packing.events.dto.*;
import com.xingda.packing.minitor.entity.PkAlarmType;
import com.xingda.packing.minitor.entity.PkEvents;
import com.xingda.packing.minitor.entity.PkTask;
import com.xingda.packing.minitor.service.IPkTaskUploadService;
import com.xingda.packing.task.ticket.CachePackingTicket;
import com.xingda.packing.task.ticket.PackingTicket;
import com.xingda.packing.task.ticket.ScanCodeTicket;
import com.xingda.packing.task.ticket.StorageTicket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Component
public class EventCreater {

    @Resource
    EventBus eventBus;

    @Resource
    IPkTaskUploadService pkTaskUploadService;

    /**
     * 保存事件
     */
    public void createPkEvent(EventType eventType, int area, Object... content) {
        PkEvents pkEvents = new PkEvents();
        StringBuilder contentBuilder = new StringBuilder();
        for (Object o : content) {
            contentBuilder.append(o);
        }
        pkEvents.setContent(contentBuilder.toString());
        pkEvents.setCreateTime(new Date());
        pkEvents.setEventType(eventType.getTypeId());
        pkEvents.setArea(area);
        eventBus.post(pkEvents);
    }

    /**
     * 抓取位放装箱位动作
     */
    public void createPkActionPacking(PackingTicket ticket, Integer area) {
        EventActionPacking actionPacking = new EventActionPacking();
        actionPacking.setTicket(ticket);
        actionPacking.setArea(area);
        eventBus.post(actionPacking);
    }
    public void createPkActionScanCode(ScanCodeTicket ticket,Integer area){
        EventActionScanCode actionScanCode=new EventActionScanCode();
        actionScanCode.setTicket(ticket);
        actionScanCode.setArea(area);
        eventBus.post(actionScanCode);
    }

    /**
     * 抓取位到缓存位动作
     */
    public void createPkActionStorage(StorageTicket ticket, Integer area) {
        EventActionStorage actionStorage = new EventActionStorage();
        actionStorage.setTicket(ticket);
        actionStorage.setArea(area);
        eventBus.post(actionStorage);
    }


    /**
     * 缓存位到装箱位动作
     */
    public void createPkActionCachePacking(CachePackingTicket ticket, Integer area) {
        EventActionCachePacking actionCachePacking = new EventActionCachePacking();
        actionCachePacking.setTicket(ticket);
        actionCachePacking.setArea(area);
        eventBus.post(actionCachePacking);
    }

    public void createFinishPkAction(ActionType actionType, int area) {
        EventActionType eventActionType = new EventActionType();
        eventActionType.setActionType(actionType);
        eventActionType.setArea(area);
        eventBus.post(eventActionType);
    }

    /**
     * 上传包装箱信息
     */
    public void createPackinvWeigh(PkTask pkTask, int boxLocationNo, int area) {

        boolean isUpload = AppSetParam.is_floor_upload && (pkTask.getType() == BoxTaskType.PACKING_FINISHED.getTypeId()) && pkTask.isUpload();
        log.info("中筒包装物信息上传,中筒码-{};是否上传本层信息:{}", pkTask.getPkPacking(), isUpload);
        createPkEvent(EventType.UPLOAD, area, "装箱位：", boxLocationNo, "，装箱号：", pkTask.getBoxNo(), "，中筒包装物信息是否上传：", isUpload);
        if (isUpload) {
            EventPackinvWeigh eventPackinvWeigh = new EventPackinvWeigh();
            eventPackinvWeigh.setPkTask(pkTask);
            eventPackinvWeigh.setBoxLocationNo(boxLocationNo);
            eventPackinvWeigh.setArea(area);
            eventBus.post(eventPackinvWeigh);
        } else {
            pkTaskUploadService.saveOrUpdate(pkTask.getId(), 0, TaskUploadState.NOT_UPLOAD.getNumber(), null, TaskUploadType.AUTO.getNumber(),TaskUploadState.NOT_UPLOAD.getDesc());
        }
    }

    /**
     * 向led屏写入字幕
     *
     * @param message 消息内容
     * @param ip      IP地址
     * @param port    端口号
     */
    public void createLedMessage(String message, String ip, int port) {
        EventLedMessage elm = new EventLedMessage();
        elm.setIp(ip);
        elm.setMessage(message);
        elm.setPort(port);
        eventBus.post(elm);
    }

    public void createTriggerAlarm(AlarmTriggerType alarmTriggerType, boolean trigger, int area) {
        EventTriggerAlarmSaveDto eventTriggerAlarmSaveDto = new EventTriggerAlarmSaveDto();
        eventTriggerAlarmSaveDto.setAlarmTriggerType(alarmTriggerType);
        eventTriggerAlarmSaveDto.setTrigger(trigger);
        eventTriggerAlarmSaveDto.setArea(area);
        eventBus.post(eventTriggerAlarmSaveDto);
    }

    public void createTriggerAlarm(PkAlarmType pkAlarmType, boolean trigger, int area) {
        EventTriggerAlarmSaveDto eventTriggerAlarmSaveDto = new EventTriggerAlarmSaveDto();
        eventTriggerAlarmSaveDto.setPkAlarmType(pkAlarmType);
        eventTriggerAlarmSaveDto.setTrigger(trigger);
        eventTriggerAlarmSaveDto.setArea(area);
        eventBus.post(eventTriggerAlarmSaveDto);
    }
}
